package com.auth.manage.user.domain.service.query;

import com.auth.manage.user.domain.service.query.strategy.UserQueryStrategy;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.InitializingBean;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@RequiredArgsConstructor
public class UserQueryFactory implements InitializingBean {

    // 所有UserQueryStrategy的具体实现类只要被Spring管理，都会注入到这个List中
    private final List<UserQueryStrategy> queryStrategyList;
    private  Map<String, UserQueryStrategy> queryStrategyMap;

    // QueryFactory进行依赖注入后，就会调用该方法
    @Override
    public void afterPropertiesSet() throws Exception {
        queryStrategyMap = new ConcurrentHashMap<>();
        for (UserQueryStrategy queryStrategy : queryStrategyList) {
            queryStrategyMap.put(queryStrategy.getStrategyCode().name(), queryStrategy);
        }
    }

    public UserQueryStrategy getQueryStrategy(String name){
        return queryStrategyMap.get(name);
    }
}
